//
//  GestureMenuViewController.swift
//  EasePods
//
//  Created by 易先生 on 2024/4/30.
//

import UIKit
import JXSegmentedView
import CRPEarPhone

class GestureMenuViewController: ViewController {

    private let titles: [CRPEarType] = [.left, .right]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigation.item.title = R.string.localizable.home_setting_gesture_control_title()
        self.view.backgroundColor = R.color.common_10_bg_page()
        
    }
    
    override func makeUI() {
        super.makeUI()
        
        view.addSubview(segmentedView)
        view.addSubview(listContainerView)
        
        segmentedView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(kNavHeight+23)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(28)
        }
        
        listContainerView.snp.makeConstraints { make in
            make.top.equalTo(segmentedView.snp.bottom).offset(16)
            make.leading.trailing.bottom.equalToSuperview()
        }
    }
    
    private lazy var segmentedView: JXSegmentedView = {
        let segmentedView = JXSegmentedView()
        segmentedView.dataSource = segmentDataSource
        segmentedView.listContainer = listContainerView
        segmentedView.indicators = [indicator]
        return segmentedView
    }()
    
    private lazy var indicator: JXSegmentedIndicatorLineView = {
        let indicaotr = JXSegmentedIndicatorLineView()
        indicaotr.indicatorColor = R.color.common_3_text() ?? .clear
        indicaotr.indicatorHeight = 2
        return indicaotr
    }()

    private lazy var segmentDataSource: JXSegmentedTitleDataSource = {
        let datasource = JXSegmentedTitleDataSource()
        datasource.titles = titles.map{ $0.title }
        datasource.titleNormalFont = Fonts.mediumSystemFont(ofSize: 14)
        datasource.titleSelectedFont = Fonts.mediumSystemFont(ofSize: 14)
        datasource.titleNormalColor = R.color.common_23_text_second() ?? .clear
        datasource.titleSelectedColor = R.color.common_3_text() ?? .clear
        datasource.isItemSpacingAverageEnabled = false
        datasource.itemSpacing = 24
        return datasource
    }()
    
    private lazy var listContainerView: JXSegmentedListContainerView = {
       let container = JXSegmentedListContainerView(dataSource: self, type: .scrollView)
        container.scrollView.backgroundColor = R.color.common_10_bg_page()
        container.listCellBackgroundColor = R.color.common_10_bg_page() ?? .clear
       container.scrollView.isScrollEnabled = false
       return container
   }()

}

extension GestureMenuViewController:  JXSegmentedListContainerViewDataSource {
    
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return titles.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        let type = self.titles[index]
        return GestureViewController(viewModel: GestureViewModel(eartype: type))
    }
}
